/**
 * Javascript source file for show.php 
 */

var searchstore = null;
var current_tooltip_column = null;
var current_tooltip_row = null;

function start_search() {
	dojo.byId("messageAreaContent").innerHTML = "";
	dojo.byId("messageArea").style.display    = "none";
	searchstore = null;
	if (dijit.byId("search1").get("open") == true) {
		dijit.byId("search1").toggle();
	}
	if (dijit.byId("search2").get("open") == true) {
		dijit.byId("search2").toggle();
	}	
	
	var xhrArgs = {
	        url: "ajax/ajax_show.php?type=search",
	        form: dojo.byId("show1"),
	        handleAs: "json",
	        load: function(jsonData) {
	        	if (jsonData.messages != null) {
	        		showMessages(jsonData.messages);
	        	}
	        	
	        	// show number of hits
	        	dojo.byId("results").innerHTML = NumberFormat(jsonData.results);
	        	dojo.byId("result_view").style.display = "block";
	        	
	        	// change store URL
        		searchstore = new dojox.data.QueryReadStore({
        			url:"ajax/ajax_show.php?type=content",
    				id:"searchstore",
    				requestMethod:"post"
    			});
        		searchgrid.setStore(searchstore);

	        	
	        },
	        error: function(error) {
				console.log("Error:");
				console.log(error);
	        }
	}	
	dojo.xhrPost(xhrArgs);	
	return false; // to avoid form submit
}

function format_center(val, rowIdx, cell) {
	cell.customClasses.push("centered_text");
	return val;
}

function format_address(val, rowIdx, cell) {
	cell.customClasses.push("centered_text");
	var coordinates = val.split(":");
	if (coordinates.length != 3) return;
	
	return '<a href="galaxyview.php?gala='+coordinates[0]+'&system='+coordinates[1]+'">'+val+'</a>';
}

function format_datetime(val, rowIdx, cell) {
	cell.customClasses.push("centered_text");
	cell.customClasses.push("datetime_text");
	//val = val.replace(/\s/g, "<br>");
	return val;
}

function format_moon(val, rowIdx, cell) {
	cell.customClasses.push("centered_text");
	if (val.exists == false) return "";
	
	return "M<br><span style=\"font-size: 8px;\">"+NumberFormat(val.size)+" km</span>";
}

function format_debris(val, rowIdx, cell) {
	cell.customClasses.push("centered_text");
	var debris = val;
	
	if (val.m > 0 || val.c > 0) {
		val = "<div id=\"debris"+rowIdx+"\" style=\"font-size: 8px;\">M:"+NumberFormat(debris.m)+"</div><div style=\"font-size: 8px;\">K:"+NumberFormat(debris.c)+"</div>";
	} else {
		return "";
	}

	return val;
}


function format_ally(val, rowIdx, cell) {
	cell.customClasses.push("centered_text");
	
	if (val.id == 0 || val.id == null) return "";
	
	var value = "<a href=\"allyinformation.php?id="+val.id+"\" class=\""+val.s+"\">"+val.n+"</a>";
	
	return value;
}

function format_player(val, rowIdx, cell) {
	cell.customClasses.push("centered_text");
	
	if (val.id == 0 || val.id == null) return "";
	
	var value = "<a href=\"playerinformation.php?id="+val.id+"\" class=\""+val.s+"\">"+val.n+"</a>";
	
	return value;
}

function format_status(val, rowIdx, cell) {
	cell.customClasses.push("centered_text");
	
	var value = "";
	if (val.indexOf("v") > -1) value += "<span class=\"vacation\">"+TEXT_PLAYER_VACATION_MODE+"</span>";
	if (val.indexOf("b") > -1) value += "<span class=\"banned\">"+TEXT_PLAYER_BANNED+"</span>";
	if (val.indexOf("i") > -1) value += "<span class=\"inactive\">"+TEXT_PLAYER_INACTIVE+"</span>";
	if (val.indexOf("I") > -1) value += "<span class=\"longinactive\">"+TEXT_PLAYER_LONG_INACTIVE+"</span>";
	if (val.indexOf("o") > -1) value += "<span class=\"outlaw\">"+TEXT_PLAYER_OUTLAW+"</span>";
	return value;
}

function format_notices(val, rowIdx, cell) {
	val = parseInt(val);
	if (!(val > 0)) return "";
	
	var content = '<img class="hyperlink" onClick="ShowNoticeContent('+val+')" src="../images/notice.gif" height="14" width="12" alt="notice" border="0">';
	return content;
}

function format_reports(val, rowIdx, cell) {
	if (val.p != true && val.m != true) return "";
	
	var margin = "10px";
	if (val.p == true) {
		var content = '<img class="hyperlink" onClick="show_report(\''+val.c+'\',false)" src="../images/spio.gif" height="16" width="13" alt="espionage report" border="0">';
	} else {
		var content = '';
		margin = "23px"; // 10 standard + 13 img width
	}
	if (val.m == true) {
		content += '<img style="margin-left: '+margin+';" class="hyperlink" onClick="show_report(\''+val.c+'\',true)" src="../images/spio.gif" height="16" width="13" alt="espionage report" border="0">';
	} else {
		content += '';
	}	
	return content;
}

function show_row_tooltip(e) {
	var show_grid = false;
	var column = e.cellIndex;
	var row    = e.rowIndex;
	if (column == current_tooltip_column && row == current_tooltip_row) {
		return;
	}
	
	// fetch value of that cell
	var value = searchgrid.store.getValue(searchgrid.getItem(row), "debris");
	
	if (column == 2) { // debris
		if (value.m == 0 && value.c == 0) return;
		var mouse_over = '<div style=font-size:10pt;>'+
		TEXT_REPORTS_FOR_TF+':<br>'+TEXT_REPORTS_REC+': '+
			NumberFormat(Math.ceil((value.m + value.c) / 20000))+'</div>';
		
		show_grid = true;
	}
	
	if (show_grid) {
		current_tooltip_column = column;
		current_tooltip_row    = row;
		dijit.showTooltip(mouse_over, e.cellNode);
	}
}

function hide_row_tooltip(e) {
	current_tooltip_column = null;
	current_tooltip_row    = null;	
	dijit.hideTooltip(e.cellNode);
}


function ShowNoticeContent(player_id) {
	dojo.byId("messageArea").style.display    = "none";
	dijit.byId("NoticeDialogContent").set("href",'ajax/ajax_notices_read.php?playerid='+player_id);
	dojo.byId("NoticeDialogEdit").setAttribute("onclick", "EditNoticeContent("+player_id+")");
	dojo.byId("NoticeDialogEdit").style.display="";
	dijit.byId("NoticeDialog").show();
}
function EditNoticeContent(player_id) {
	dijit.byId("NoticeDialogContent").set("href",'ajax/ajax_notices_edit.php?playerid='+player_id+"&dialog=NoticeDialog&refresh=");
	dojo.byId("NoticeDialogEdit").style.display="none";
}

function galaxytool_show_init() {
	// enable browser-autocomplete - has to be done here, see
	// http://bugs.dojotoolkit.org/ticket/9562
	dojo.attr(dijit.byId('player').textbox, "autocomplete", "on");
	dojo.attr(dijit.byId('alliance').textbox, "autocomplete", "on");
	
	dojo.connect(searchgrid, "onRowMouseOver", show_row_tooltip);
	dojo.connect(searchgrid, "onRowMouseOut", hide_row_tooltip);
	
	if (TRIGGER_SEARCH == true) start_search(); else dijit.byId('player').focus();
}

dojo.ready(galaxytool_show_init);
